package cn.fangline.wo.task.akka.actors;

import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import cn.fangline.wo.model.House;
import cn.fangline.wo.model.OriginHouse;
import cn.fangline.wo.task.akka.messages.EndPoint;
import cn.fangline.wo.task.akka.messages.HouseImagePage;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by 抽离 on 2017/6/26 0026.
 */
public class HouseInsertActor extends AbstractActor {

    private ActorRef houseImageMasterRef;

    public HouseInsertActor(ActorRef houseImageMasterRef) {
        this.houseImageMasterRef = houseImageMasterRef;
    }

    @Override
    public Receive createReceive() {
        return
                receiveBuilder()
                        .match(List.class, this::onReceiveInsertHouseList)
                        .build();
    }

    private void onReceiveInsertHouseList(List<OriginHouse> originHouses) {//到达这里的originHouse都携带userId和corpId，但是没有houseId
        for(OriginHouse originHouse : originHouses) {
            if (originHouse.getArea() == null || originHouse.getTotalPrice() == null) {
                return;
            }
            Float areaUp = originHouse.getArea() + 0.5f;
            Float areaDown = originHouse.getArea() - 0.5f;

            Float totalPriceUp = originHouse.getTotalPrice() * 1.01f;
            Float totalPriceDown = originHouse.getTotalPrice() * 0.99f;

            House house = new House();
            List<House> houses = house.selectList(
                    new EntityWrapper<House>()
                            .eq("section", originHouse.getSection())
                            .eq("corp_id", originHouse.getCorpId())
                            .eq("city_id", originHouse.getCityId())
                            .eq("room", originHouse.getRoom())
                            .between("area", areaDown, areaUp)
                            .between("total_price", totalPriceDown, totalPriceUp)
            );

            if(houses != null && houses.size() > 0){//已经有该房源，就不需要爬取图片了
                House h = houses.get(0);
                originHouse.setHouseId(h.getHouseId());
                originHouse.insert();

                if(!originHouse.getTotalPrice().equals(h.getTotalPrice())){
                    h.setTotalPrice(originHouse.getTotalPrice());
                }
                h.setOriginCount(h.getOriginCount() + 1);
                h.updateById();
//                getSender().tell(new EndPoint(), getSelf());
            }
            else {
                House hh = new House();
                hh.setCityId(originHouse.getCityId());
                hh.setSection(originHouse.getSection());
                hh.setRoom(originHouse.getRoom());
                hh.setHall(originHouse.getHall());
                hh.setToilet(originHouse.getToilet());
                hh.setArea(originHouse.getArea());
                hh.setTotalPrice(originHouse.getTotalPrice());
                hh.setUnitPrice(originHouse.getUnitPrice());
                hh.setImgUrl(originHouse.getImgUrl());
                hh.setCorpId(originHouse.getCorpId());
                hh.setCreateTime(new Date());
                hh.setFloor(originHouse.getFloor());
                hh.setTowards(originHouse.getTowards());
                hh.setAddress(originHouse.getAddress());
                hh.setOriginHouseId(originHouse.getOriginHouseId());
                hh.setOriginCount(1);
                hh.setUpdateTime(originHouse.getUpdateTime());
                hh.insert();

                originHouse.setHouseId(hh.getHouseId());
                originHouse.insert();
                HouseImagePage houseImagePage = new HouseImagePage();
                houseImagePage.setCounter(1);
                houseImagePage.setHouseId(hh.getHouseId());
                houseImagePage.setCityId(hh.getCityId());
                houseImagePage.setUrl(originHouse.getUrl());
                houseImagePage.setOriginHouseId(hh.getOriginHouseId());
                houseImageMasterRef.tell(houseImagePage, getSelf());
            }

        }
    }
}
